package com.cl.short_video_java.web.client.video;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cl.short_video_java.annotation.Login;
import com.cl.short_video_java.annotation.NoRepeatSubmit;
import com.cl.short_video_java.common.Const;
import com.cl.short_video_java.common.Result;
import com.cl.short_video_java.dto.VideoDetail;
import com.cl.short_video_java.exception.WebException;
import com.cl.short_video_java.pojo.VideoUser;
import com.cl.short_video_java.pojo.VideoVideo;
import com.cl.short_video_java.service.VideoHistoryService;
import com.cl.short_video_java.service.VideoUserBuyService;
import com.cl.short_video_java.service.VideoUserService;
import com.cl.short_video_java.service.VideoVideoService;
import com.cl.short_video_java.util.RedisUtil;
import com.cl.short_video_java.vo.video.VideoSearch;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author 付
 * @Date 2024-11-20 16:03
 * @Version 1.0
 * @description
 * @since 1.0
 */

@RestController
@RequestMapping("/client/video")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ClientVideoController {

	private final VideoUserBuyService buyService;
	private final VideoHistoryService historyService;
	private final VideoVideoService videoService;
	private final RedisUtil redisUtil;
	private final VideoUserService userService;

	/**
	 * 获取视频购买记录
	 *
	 * @param pageNum
	 * @param request
	 * @return
	 */
	@GetMapping("/buy/recording")
	public Result getRecording(Integer pageNum, HttpServletRequest request) {
		return Result.success(buyService.getRecording(pageNum, request));
	}

	/**
	 * 获取观看记录
	 *
	 * @param pageNum
	 * @param request
	 * @return
	 */
	@GetMapping("/watch/recording")
	public Result getWatchRecording(Integer pageNum, HttpServletRequest request) {
		return Result.success(historyService.getRecording(pageNum, request));
	}

	/**
	 * 观看记录删除
	 *
	 * @param id 记录id
	 * @return
	 */
	@DeleteMapping("/watch/del")
	@NoRepeatSubmit(lockTime = 1)
	@Transactional(rollbackFor = Exception.class)
	public Result delWatchRecording(@RequestParam("id") Integer id) {
		historyService.removeById(id);
		return Result.success();
	}

	/**
	 * 获取首页的推荐列表
	 *
	 * @param pageNum
	 * @return
	 */
	@GetMapping("/list")
	public Result getList(@RequestParam("pageNum") Integer pageNum) throws JsonProcessingException {
		List<?> bank = videoService.getRecommon(pageNum);
		return Result.success(MapUtil.builder().put("total", redisUtil.get("recommend_total")).put("list", bank).build());
	}

	@PostMapping("/page")
	@NoRepeatSubmit(lockTime = 1)
	public Result getPage(@RequestBody VideoSearch search) {
		Page<VideoVideo> page = videoService.getPage(search);
		return Result.success(page);
	}

	/**
	 * 根据用户获取对应的视频列表
	 *
	 * @param uid     用户id
	 * @param current 页码
	 * @return
	 */
	@GetMapping("/user/video")
	public Result video(@RequestParam("uid") Integer uid, @RequestParam("current") Integer current) {
		Page<VideoVideo> page = videoService.videoByUser(uid, current);
		return Result.success(page);
	}


	/**
	 * 判断视频类型以及用户是否符合条件
	 *
	 * @param vid
	 * @param request
	 * @return
	 */
	@GetMapping("/type")
	public Result type(@RequestParam("vid") Integer vid, HttpServletRequest request) {
		VideoUser user = userService.getCurrentUser(request, Const.GRADE_USER);
		boolean b = videoService.getType(vid, user);

		return Result.success(b);
	}

	@GetMapping("/info")
	public Result info(@RequestParam("vid") Integer vid, HttpServletRequest request) {
		VideoUser user = userService.getCurrentUser(request, Const.GRADE_USER);
		VideoDetail video = videoService.info(vid, user.getId());
		return Result.success(video);
	}

	/**
	 * 用户点赞
	 *
	 * @param vid
	 * @param request
	 * @return
	 */
	@GetMapping("/like")
	@Login(issuer = Const.GRADE_USER)
	@NoRepeatSubmit(lockTime = 2)
	public Result like(@RequestParam("vid") Integer vid, HttpServletRequest request, Integer model) {
		VideoUser user = userService.getCurrentUser(request, Const.GRADE_USER);
		videoService.like(vid, user.getId(), model);
		return Result.success();
	}

	/**
	 * 用户收藏
	 *
	 * @param vid
	 * @param request
	 * @param model   类型：1-收藏，2-取消收藏
	 * @return
	 */
	@GetMapping("/collect")
	@Login(issuer = Const.GRADE_USER)
	@NoRepeatSubmit(lockTime = 2)
	public Result collect(@RequestParam("vid") Integer vid, HttpServletRequest request, Integer model) {
		VideoUser user = userService.getCurrentUser(request, Const.GRADE_USER);
		videoService.collect(vid, user.getId(), model);
		return Result.success();
	}

	/**
	 * 购买视频
	 *
	 * @param vid
	 * @param request
	 * @return
	 */
	@GetMapping("/buy")
	@Login(issuer = Const.GRADE_USER)
	@NoRepeatSubmit(lockTime = 3)
	public Result buy(@RequestParam("vid") Integer vid, HttpServletRequest request) {
		VideoUser user = userService.getCurrentUser(request, Const.GRADE_USER);
		videoService.buy(vid, user);
		return Result.success();
	}
}
